package com.immunology.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.immunology.core.BaseDao;
import com.immunology.core.BaseTransformer;
import com.immunology.core.GenericDao;
import com.immunology.database.DbColumns;
import com.immunology.database.transformers.SurveyTransformer;
import com.immunology.model.Survey;

public class SurveyDao extends BaseDao<Survey> implements GenericDao<Survey> {

	public SurveyDao(BaseTransformer<Survey> transformer) {
		super(transformer, new String("SURVEY"));
	}

	@Override
	public void insert(Survey survey) {
		Connection connection = connectionManager.getConnection();
		StringBuilder query = new StringBuilder();
		query.append("INSERT INTO ");
		query.append(tableName);
		query.append(" (doctor_id, date, medicine_card_id, difficulty_degree, insufficiency_degree)");
		query.append(" VALUES(?, ?, ?, ?, ?)");
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(query.toString());
			preparedStatement.setInt(DbColumns.FIRST_COLUMN,
					survey.getDoctorId());
			preparedStatement
					.setDate(DbColumns.SECOND_COLUMN, survey.getDate());
			preparedStatement.setInt(DbColumns.THIRD_COLUMN,
					survey.getMedicineCardId());
			preparedStatement.setInt(DbColumns.FOURTH_COLUMN,
					survey.getDifficultyDegree());
			preparedStatement.setInt(DbColumns.FIFTH_COLUMN,
					survey.getInsufficiencyDegree());

			preparedStatement.executeUpdate();
			preparedStatement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionManager.closeConnection(connection);
		}
	}

	@Override
	public void update(int id, Survey survey) {

		Connection connection = connectionManager.getConnection();
		StringBuilder query = new StringBuilder();
		query.append("UPDATE ");
		query.append(tableName);
		query.append(" SET doctor_id = ?, date = ?, medicine_card_id = ?, difficulty_degree = ?, insufficiency_degree = ?");
		query.append(" WHERE id = ?");
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = (PreparedStatement) connection
					.prepareStatement(query.toString());
			preparedStatement.setInt(DbColumns.FIRST_COLUMN,
					survey.getDoctorId());
			preparedStatement
					.setDate(DbColumns.SECOND_COLUMN, survey.getDate());
			preparedStatement.setInt(DbColumns.THIRD_COLUMN,
					survey.getMedicineCardId());
			preparedStatement.setInt(DbColumns.FOURTH_COLUMN,
					survey.getDifficultyDegree());
			preparedStatement.setInt(DbColumns.FIFTH_COLUMN,
					survey.getInsufficiencyDegree());
			preparedStatement.setInt(DbColumns.SIXTH_COLUMN, survey.getId());

			preparedStatement.executeUpdate();
			preparedStatement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionManager.closeConnection(connection);
		}

	}

	public int getLastSurveyId() {
		Connection connection = connectionManager.getConnection();
		int id = 0;
		StringBuilder query = new StringBuilder();
		query.append("SELECT id FROM ");
		query.append(tableName);
		query.append(" ORDER BY id");
		try {
			Statement statement = connection.createStatement();
			ResultSet set = statement.executeQuery(query.toString());
			while (set.next()) {
				id = set.getInt(DbColumns.FIRST_COLUMN);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionManager.closeConnection(connection);
		}
		return id;
	}

	public List<Survey> getSurveyListByMedicineCardId(int medicineCardId) {
		List<Survey> surveyList = new ArrayList<Survey>();

		Connection connection = connectionManager.getConnection();
		ResultSet resultSet = null;
		StringBuilder query = new StringBuilder();
		query.append("SELECT * FROM ");
		query.append(tableName);
		query.append(" WHERE medicine_card_id = ?");
		query.append(" ORDER BY id");
		try {
			PreparedStatement preparedStatement = connection
					.prepareStatement(query.toString());
			preparedStatement.setInt(DbColumns.FIRST_COLUMN, medicineCardId);
			resultSet = preparedStatement.executeQuery();
			SurveyTransformer surveyTransformer = new SurveyTransformer();
			while (resultSet.next()) {
				surveyList.add(surveyTransformer.transformTo(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionManager.closeConnection(connection);
		}
		return surveyList;
	}

}
